<?php
class AutoCDbMessageSource extends CDbMessageSource{
	public function translate($category,$message,$language=null)
	{
		$sql="SELECT `id` FROM sourcemessage WHERE `category`=:category AND `message`=:message";
		$command=Yii::app()->db->createCommand($sql);
		$command->bindParam(":category",$category,PDO::PARAM_STR);
		// replace the placeholder ":email" with the actual email value
		$command->bindParam(":message",$message,PDO::PARAM_STR);
		$messageId = $command->queryScalar();
		if(!isset($messageId) || $messageId < 1){
			//Insert message to database
			$sql="INSERT INTO sourcemessage (`id`, `category`, `message`) VALUES(NULL,:category,:message)";
			$command=Yii::app()->db->createCommand($sql);
			// replace the placeholder ":username" with the actual username value
			$command->bindParam(":category",$category,PDO::PARAM_STR);
			// replace the placeholder ":email" with the actual email value
			$command->bindParam(":message",$message,PDO::PARAM_STR);
			$command->execute();
		}		
		
		if($language===null)
			$language=Yii::app()->getLanguage();
		if($this->forceTranslation || $language!==$this->getLanguage())
			return $this->translateMessage($category,$message,$language);
		else
			return $message;
	}
}